/** ==================================================================================== **/
/** 				 			 Service per la gestione dei log						 **/
/** ==================================================================================== **/

/**
 * E' possibile settarne il livello in fase di run in modo da lasciarne attivo un solo livello (ad es. ERROR) in produzione
 */
BootApplication.service("log", ['dateFilter', function(dateFilter) {
	
	var logLevels = { all : 0, debug : 1, info : 2, warning : 3, error : 4, off : 5 };
	var logLevel  = logLevels.all;
	var isDebug   = true;
	var isInfo    = true;
	var isWarning = true;
	var isError   = true;
	
	function dateFormatter(date) {
		var val = "No Date.";
		if(date instanceof Date) {
			val = dateFilter(date, 'dd/MM/yyyy HH:mm:ss');
		}
		return val;
	};
	
	function text(txt, object) {
		var jsonObject = '';
		
		if(object) jsonObject = '\n' + JSON.stringify(object);
		
		return dateFormatter(new Date()) + " - " + txt + jsonObject;
	};
	
	return {
		info : function(txt, obj) {
			if(isInfo) {
				console.log("INFO: " + text(txt, obj));
			}
		},
		error : function(txt, obj) {
			if(isError) {
				console.error("ERROR: " + text(txt, obj));
			}
		},
		warning : function(txt, obj) {
			if(isWarning) {
				console.warn("WARNING: " + text(txt, obj));
			}
		},
		debug : function(txt, obj) {
			if(isDebug) {
				console.debug("DEBUG: " + text(txt, obj));
			}
		},
		getLevels : function() {
			return logLevels;
		},
		setLevel : function(level) {
			logLevel = level;
			isDebug   = logLevel <= logLevels.debug;
			isInfo    = logLevel <= logLevels.info
			isWarning = logLevel <= logLevels.warning
			isError   = logLevel <= logLevels.error
		}
	}
}]);